/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.femass.repository.infra;

import com.femass.model.Artigo;
import com.femass.model.Autor;
import com.femass.model.Avaliacao;
import com.femass.model.Revisor;
import com.femass.model.Tema;
import com.femass.repository.AvaliacaoRepository;
import com.femass.util.FacesUtil;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author daureo
 */
public class AvaliacaoRepositoryHibernate implements AvaliacaoRepository{

    @Override
    public List<Avaliacao> todas() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Avaliacao> porTema(Tema tema) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Avaliacao> porArtigo(Artigo artigo) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Avaliacao> porAutor(Autor autor) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void salvar(Avaliacao avaliacao) {
        Session session = (Session) FacesUtil.getRequestAttribute("session");
       try{
            session.persist(avaliacao);                       
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_INFO, "Avaliação Salva!");
       } catch(Exception e){
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_WARN, "Erro ao salvar Avaliação!");
       }
    }

    @Override
    public Avaliacao avaliadoPorRevisor(Avaliacao avaliacao) {
         Session session = (Session) FacesUtil.getRequestAttribute("session");
         return (Avaliacao) session.createCriteria(Avaliacao.class)
                .add(Restrictions.eq("revisor", avaliacao.getRevisor()))
                .add(Restrictions.eq("artigo", avaliacao.getArtigo()))
                .uniqueResult();
    }
    
}
